/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bits.user;

import java.sql.ResultSet;
import com.bits.db.DBConnection;


/**
 * Maintains the User Profile
 * @author 3GBITS
 */
public class UserProfileManager {


    private DBConnection connection = null;
//    private User user = null;
   
    private ResultSet rs = null;

    public UserProfileManager() {
            connection = new DBConnection();
    }
    


    /**
     * Creates a New User
     * @return - true if new user created otherwise false.
     */
    public boolean createUser(User user) {

    
       
        if (!isUserNameAvailable(user.getUserName())) {
            return false;
        }
        try {
            String query1 = "INSERT INTO USERS VALUES(\'"+user.getUserName()+"\',\'"+user.getPassword()+"\')";
            connection.insertOrUpdate(query1);
            String query2 = "INSERT INTO USER_PROFILE VALUES(\'" + user.getUserName()+ "\',\'" + user.getFirstName() + "\',\'" + user.getLastName() + "\',\'" + user.getMobile() + "\',\'" +
                    user.getEmail() + "\',\'" + user.getAddress() + "\',\'"
                    + user.getPhone() + "\',\'" + user.getCity() + "\')";
//       ResultSet rs = connection.getResultSet(query);
            connection.insertOrUpdate(query2);
        } catch (Exception e) {
            System.out.println("UserProfile.java : in createNewUser()" + e);
        }

        return true;
    }

      /**
     * Checks the availability of the username from the DB.
     * @return True if username is available , otherwise false
     */
    public boolean isUserNameAvailable(String userName) {
        boolean status = true;

        String query = "select username from users where username=\'" + userName + "\'";
        try {
            rs = connection.getResultSet(query);
            if (rs.next()) {
                status = false;
            } else {
                status = true;
            }
            if(rs != null)
            rs.close();


        } catch (Exception e) {
            System.out.println(e);
            connection.cleanAll();
        }
        return status;
    }

    public void clean(){
        connection.cleanAll();
    }


    /**
 * Modifies the User Profile.
 * @return
 */
    public boolean modifyUser(User user){




        return true;
    }
    public void dropUser(User user){
        
    }

 
}
